﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using RobotUI.Application;
using System.Windows.Automation;
using RobotUI.Controls;


namespace RobotUI
{
    public class Keywords
    {

    	private AppUnderTest _app = AppUnderTest.getInstance();
    	
        #region Application

        /// <summary>
        /// Starts an exe windows application with cmd line arguments
        /// </summary>
        public void application_open_withargs(String exepath, String arguments)
        {
        	_app.Start(exepath,arguments);
        }

        /// <summary>
        /// Starts an exe windows application without cmd line arguments
        /// </summary>
        public void application_open(String exepath)
        {
        	_app.Start(exepath);
        }


        /// <summary>
        /// Kills the application
        /// </summary>
        public void application_kill()
        {
        	_app.Kill();
        }
        
        /// <summary>
        /// Activates and gives focus to the main window of the application
        /// </summary>
        public void application_set_focus_main_window()
        {
        	_app.ActivateMainWindow();
        }
        
        /// <summary>
        /// Activates and gives focus to a child window of the application with Title matching regular expression
        /// </summary>
        public void application_set_focus_child_window(String regex)
        {
        	_app.ActivateChildWindow(regex);
        }
        
        /// <summary>
        /// Checks if the application has a child window with a Title matching the regular expression.
        /// Returns True/False
        /// </summary>
        public String application_has_child_window(String regex)
        {
        	return _app.HasChildWindow(regex).ToString();
        }
        
        /// <summary>
        /// Waits until any child window is found
        /// If no child window match found within the specified seconds, the keyword fails
        /// </summary>
        public void application_wait_for_any_child_window(String seconds)
        {
        	if (String.IsNullOrEmpty(seconds)) throw new KeywordException("Invalid seconds parameter");
        	int secs = int.Parse(seconds);
        	_app.WaitForChildWindow(null,secs);
        }
        
        /// <summary>
        /// Waits until a child window is found with a Title matching the regular expression.
        /// If no child window match found within the specified seconds, the keyword fails
        /// </summary>
        public void application_wait_for_child_window(String regex, String seconds)
        {
        	if (String.IsNullOrEmpty(seconds)) throw new KeywordException("Invalid seconds parameter");
        	int secs = int.Parse(seconds);
        	_app.WaitForChildWindow(regex,secs);
        }
        
        /// <summary>
        /// Checks if the application has a dialog window currently open and returns True/False
        /// </summary>
        public String application_has_dialog_window()
        {
        	return _app.HasDialogWindow().ToString();
        }
        
        /// <summary>
        /// Waits until the application displays any dialog window.
        /// If none found within specified seconds keyword fails
        /// </summary>
        public void application_wait_for_any_dialog_window(String seconds)
        {
        	if (String.IsNullOrEmpty(seconds)) throw new KeywordException("Invalid seconds parameter");
        	int secs = int.Parse(seconds);
        	_app.WaitForDialogWindow(null,secs);
        }
        
        /// <summary>
        /// Waits until the application displays a dialog window with Title matching regular expression.
        /// If none found within specified seconds keyword fails
        /// </summary>
        public void application_wait_for_dialog_window(String regex,String seconds)
        {
        	if (String.IsNullOrEmpty(seconds)) throw new KeywordException("Invalid seconds parameter");
        	int secs = int.Parse(seconds);
        	_app.WaitForDialogWindow(regex,secs);
        }

        /// <summary>
        /// Activates and sets focus to application dialog window.
        /// Keyword fails if application has no dialog window
        /// </summary>
        public void application_set_focus_dialog_window()
        {
        	_app.ActivateDialogWindow();
        }
        
        /// <summary>
        /// Gets the title of the window currently with focus
        /// </summary>
        public string application_get_window_title()
        {
        	return _app.GetActiveWindowTitle();
        }
        
        /// <summary>
        /// Waits until the application has no dialog windows.
        /// If the application has a dialog window after seconds, the keyword fails
        /// </summary>
        /// <param name="seconds"></param>
        public void application_wait_for_no_dialog_window(String seconds)
        {
        	if (String.IsNullOrEmpty(seconds)) throw new KeywordException("Invalid seconds parameter");
        	int secs = int.Parse(seconds);
        	_app.WaitForNoDialogWindow(secs);
        }
        
        
        /// <summary>
        /// Waits for the application to be not busy
        /// </summary>
        public void application_syncronise()
        {
        	_app.Syncronise();
        }
        
        /// <summary>
        /// Closes the main window of the application.
        /// Does not wait for the application process to terminate.
        /// </summary>
        public void application_close_main_window()
        {
        	_app.CloseMainWindow();
        }
        
        /// <summary>
        /// Waits for the application process to terminate.
        /// If the application process is still running after seconds, the keyword fails
        /// </summary>
        /// <param name="seconds"></param>
        public void application_wait_for_terminate(String seconds)
        {
        	if (String.IsNullOrEmpty(seconds)) throw new KeywordException("Invalid seconds parameter");
        	int secs = int.Parse(seconds);
        	_app.WaitForTerminate(secs);
        }
        
        
        #endregion

        #region SetValue

        /// <summary>
        /// Sets the value of a control
        /// </summary>
        public void SetValue(string id, string data)
        {
            var el = _app.GetElement(id);
            var ctrl = ElementAdapter.Adapt(el);
            ctrl.SetValue(data);
            _app.Syncronise();		
        }
        
        #endregion

        #region GetValue

        /// <summary>
        /// Gets the current value of the control
        /// </summary>>
        public string GetValue(string id)
        {
            var el = _app.GetElement(id);
            var ctrl = ElementAdapter.Adapt(el);
            var result = ctrl.GetValue();
            _app.Syncronise();
            return result;	    
        }

        #endregion

        #region Click

        /// <summary>
        /// Clicks control with left mouse
        /// </summary>
        public void Click(String id)
        {
            
        	var el = _app.GetElement(id);
        	var ctrl = ElementAdapter.Adapt(el);
        	ctrl.Click();
        	_app.Syncronise();
        	
        }
        
        #endregion


    }
}
